package com.hoshiibuy.spu.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.spu.entity.ExtSpuTags;
import com.hoshiibuy.spu.param.ExtSpuTagsPageParam;
import com.hoshiibuy.spu.param.ExtSpuTagsUpdateParam;
import com.hoshiibuy.spu.service.IExtSpuTagsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * SPU服务标签Controller类
 *
 * @author : yangcheng
 * @date : 2019/5/5 14:24
 * @mail : maiboyc@163.com
 **/
@RestController
@RequestMapping("/spuTags")
@Api(value = "SPU服务标签管理", tags = {"SPU服务标签操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class ExtSpuTagsController extends BaseController {

    // SPU服务标签Service
    @Autowired
    private IExtSpuTagsService extSpuTagsService;

    /**
     * 根据ID查询SPU服务标签资料信息
     * @param id SPU服务标签资料ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询SPU服务标签资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "SPU服务标签资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response get(Long id) {
        return Response.data(extSpuTagsService.get(id));
    }

    /**
     * 根据标签名称查询SPU服务标签资料信息
     * @param name  SPU服务标签名称
     * @return Response
     */
    @GetMapping("/{name}")
    @ApiOperation(value = "根据标签名称查询", notes = "根据标签名称查询SPU服务标签资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "name", value = "SPU服务标签名称", required = true, dataType = "String", example = "衬衫", paramType = "query")
    public Response findSpuByName(String name) {
        Wrapper wrapper = Wrappers.<ExtSpuTags>query().lambda().eq(ExtSpuTags::getTagname, name);
        return Response.data(extSpuTagsService.getOne(wrapper));
    }

    /**
     * 分页查询SPU服务标签资料信息（get请求，以具体值接收参数）
     * @param pageIndex  当前页
     * @param pageSize   每页大小
     * @param spuId      spuId
     * @param tagName    标签名称
     * @param tagValue   标签值
     * @return Response
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询SPU服务标签资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "Long", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, dataType = "Long", example = "10", paramType = "query"),
            @ApiImplicitParam(name = "spuId", value = "spuId", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "tagName", value = "标签名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagValue", value = "标签值", dataType = "String", paramType = "query")
    })
    public Response list(Long pageIndex, Long pageSize, Long spuId, String tagName, String tagValue) {
        ExtSpuTags entity = new ExtSpuTags();
        entity.setPageIndex(pageIndex);
        entity.setPageSize(pageSize);
        entity.setSpuid(spuId);
        entity.setTagname(tagName);
        entity.setTagValue(tagValue);
        return Response.data(extSpuTagsService.List(entity));
    }

    /**
     * 分页查询SPU服务标签资料信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询SPU服务标签资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response list(@RequestBody ExtSpuTagsPageParam param) {
        ExtSpuTags entity = new ExtSpuTags();
        BeanUtils.copyProperties(param, entity);
        return Response.data(extSpuTagsService.List(entity));
    }

    /**
     * 新增SPU服务标签资料
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增SPU服务标签资料", notes = "新增SPU服务标签资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response saveSpu(@RequestBody @Valid ExtSpuTags entity, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(extSpuTagsService.save(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 修改SPU服务标签资料
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改SPU服务标签资料", notes = "修改SPU服务标签资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response updateSpuById(@RequestBody @Valid ExtSpuTagsUpdateParam param, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                ExtSpuTags entity = new ExtSpuTags();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(extSpuTagsService.update(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 根据ID删除SPU服务标签资料信息
     * @param id SPU服务标签资料ID
     * @return Response
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除SPU服务标签资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "SPU服务标签资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response deleteById(Long id) {
        return Response.data(extSpuTagsService.delete(id));
    }
}  